const models = require("../db/mysqldb/index");
const { Op } = require("sequelize");

class DataSystemMenuModel {
  /**
   * @description 通过 menu_id 查找数据，不包含删除数据
   * @param {string} menu_id
   * @returns
   */
  static async findBySystemMenuId (menu_id) {
    return await models.sys_menu.findOne({
      where: {
        menu_id,
      },
    });
  }
  /**
   * @description 通过 menu_name 查找数据，不包含删除数据
   * @param {string} menu_name
   * @returns
   */
  static async findBySystemMenuName ({ menu_name }) {
    return await models.sys_menu.findOne({
      where: {
        menu_name,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 menu_name id 查找数据，不包含删除数据
   * @param {string} menu_name
   * @param {string} menu_id
   * @returns
   */
  static async findBySystemMenuNameNeId ({ menu_name, menu_id }) {
    return await models.sys_menu.findOne({
      where: {
        menu_name,
        del_flag: "0",
        menu_id: {
          [Op.ne]: menu_id,
        },
      },
    });
  }
  /**
   * @description create
   * @param {Number} parent_id
   * @param {String} menu_name
   * @param {Number} order_num
   * @param {String} path
   * @param {String} component
   * @param {String} query
   * @param {Number} is_frame
   * @param {Number} is_cache
   * @param {String} menu_type
   * @param {String} visible
   * @param {String} status
   * @param {String} perms
   * @param {String} icon
   * @param {String} remark
   * @param {String} op_user_id
   * @returns
   */
  static async create ({
    parent_id,
    menu_name,
    order_num,
    path,
    component,
    query,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    remark,
    op_user_id,
  }) {
    return await models.sys_menu.create({
      parent_id,
      menu_name,
      order_num,
      path,
      component,
      query,
      is_frame,
      is_cache,
      menu_type,
      visible,
      status,
      perms,
      icon,
      remark,
      create_by: op_user_id,
      create_time: Date.now(),
    });
  }
  /**
   * @description 更新
   * @param {Number} parent_id
   * @param {String} menu_name
   * @param {Number} order_num
   * @param {String} path
   * @param {String} component
   * @param {String} query
   * @param {Number} is_frame
   * @param {Number} is_cache
   * @param {String} menu_type
   * @param {String} visible
   * @param {String} status
   * @param {String} perms
   * @param {String} icon
   * @param {String} remark
   * @param {String} op_user_id
   * @param {Number} menu_id
   * @returns
   */
  static async update ({
    parent_id,
    menu_name,
    order_num,
    path,
    component,
    query,
    is_frame,
    is_cache,
    menu_type,
    visible,
    status,
    perms,
    icon,
    remark,
    op_user_id,
    menu_id,
  }) {
    return await models.sys_menu.update(
      {
        parent_id,
        menu_name,
        order_num,
        path,
        component,
        query,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        remark,
        update_by: op_user_id,
        update_time: Date.now(),
      },
      {
        where: { menu_id },
      }
    );
  }
  /**
   * @description 删除
   * @param {*} op_user_id
   * @param {*} menu_id
   * @returns
   */
  static async delete ({ op_user_id, menu_id }) {
    return await models.sys_menu.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: { menu_id },
      }
    );
  }

  /**
   * @description 批量删除
   * @param {Number} op_user_id
   * @param {Array} ids
   * @returns
   */
  static async deleteByIds ({ op_user_id, ids }) {
    return await models.sys_menu.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: {
          menu_id: {
            [Op.in]: ids,
          },
        },
      }
    );
  }

  /**
   * @description 获取菜单信息
   * @param {String} menu_name
   * @param {String} is_frame
   * @returns
   */
  static async list ({ menu_name, status }) {
    let querySql = "";
    if (menu_name) {
      querySql += ` and menu_name like '%${menu_name}%'`;
    }
    if (status) {
      querySql += ` and status = '${status}'`;
    }
    let sql = `SELECT
        p.*
      FROM
        sys_menu p WHERE p.del_flag = '0' ${querySql}
      ORDER BY order_num ASC,
    create_time DESC,
    update_time DESC `;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }

  /**
   * @description 获取菜单信息
   * @returns
   */
  static async listByUserId ({ user_id, menu_name, status }) {
    let querySql = "";
    if (menu_name) {
      querySql += ` and menu_name like '%${menu_name}%'`;
    }
    if (status) {
      querySql += ` and status = '${status}'`;
    }
    let sql = `select * from sys_menu where menu_id in (
    select menu_id from sys_role_menu WHERE role_id in (
      select role_id from sys_user_role where user_id = ${user_id}
    )) and del_flag = '0' ${querySql} order by order_num ASC,create_time DESC,
    update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }


  /**
   * @description 获取菜单信息
   * @returns
   */
  static async routeList () {
    // todo 需要修改
    let sql = `SELECT
        p.*
      FROM
        sys_menu p WHERE p.del_flag = '0' and p.menu_type in ('M','C') and status = '1'
      ORDER BY order_num ASC,
    create_time DESC,
    update_time DESC `;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return result;
  }

  /**
   * @description 获取菜单信息
   * @returns
   */
  static async findBySystemMenuId ({ menu_id }) {
    return await models.sys_menu.findOne({
      where: {
        menu_id,
        del_flag: "0",
      },
    });
  }

  /**
   * @description 获取菜单信息
   * @returns
   */
  static async routeListByUserId ({ user_id }) {
    let sql = `select * from sys_menu where menu_id in (
    select menu_id from sys_role_menu WHERE role_id in (
      select role_id from sys_user_role where user_id = ${user_id}
    )) and menu_type in ('M','C') and del_flag = '0' and status = '1'`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return result;
  }

  /**
   * @description 获取所有按钮信息
   * @returns
   */
  static async findAllPermission () {
    let sql = `SELECT perms as permission FROM sys_menu 
      WHERE del_flag = '0' and menu_type = 'F' and perms != '' and status = '1' and perms <>''`;
    return await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
  }

  /**
   * 根据角色id获取菜单信息
   */
  static async findMenuByRoleIds ({ ids }) {
    let sql = `
    SELECT menu_id,menu_name,parent_id,order_num,path,component,
      query,is_frame,is_cache,menu_type,visible,status,perms,icon,remark
    from sys_menu 
    WHERE menu_id in (SELECT menu_id from sys_role_menu WHERE role_id in (${ids})) 
      and status = '1'`;
    return await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
  }
  static async findBySystemRoleId ({ role_id }) {
    let sql = `
    SELECT srm.menu_id
    from sys_role_menu srm left join sys_menu sm on srm.menu_id = sm.menu_id
    WHERE srm.role_id = ${role_id}
      and sm.status = '1' `;
    return await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
  }

  /**
   * @description 获取所有菜单信息
   * @returns
   */
  static async findAllMenu () {
    let sql = `SELECT menu_id as id,menu_name as label,parent_id FROM sys_menu 
    WHERE del_flag = '0'`;
    return await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
  }
}
module.exports = {
  DataSystemMenuModel,
};
